import React from 'react';
import { inject, observer } from 'mobx-react';
import { WeaTop, WeaRightMenu, WeaSlideModal, WeaNewScroll, WeaTab, WeaAlertPage, WeaLocaleProvider, WeaTools } from 'ecCom';
import { toJS } from "mobx"
import TaskCard from '../../projectBoard/TaskCard';
import BoardApp from "./BoardApp";
import { Spin, Button, Modal } from 'antd';
const getLabel = WeaLocaleProvider.getLabel;
@inject("prjBoardStore")
@observer
class TaskBoard extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            workPlanVisible: false,
        }
    }
    componentDidMount() {
        const { prjBoardStore, prjid } = this.props;
        prjBoardStore.initList(prjid);
    }

    componentWillReceiveProps(nextProps){
        const keyOld = this.props.prjid;
        const keyNew = nextProps.prjid;
        if(keyOld !== keyNew) {
            const {prjBoardStore,prjid} = nextProps;
            prjBoardStore.initList(prjid);
        }
    }
    render() {
        let _this = this;
        const { prjBoardStore } = this.props;
        const { prjTaskCardStore,tabsStore,prjCardStore,prjid} = prjBoardStore;
        const account = WeaTools.ls.getJSONObj('theme-account');
        if (tabsStore.verified) {
            let viewObj = function (key) {
                return <BoardApp ecId={`${this && this.props && this.props.ecId || ''}_BoardApp@r7moep`}  key={prjBoardStore.refelsh+''+prjBoardStore.prjid}  boardStore={prjBoardStore}></BoardApp>
            }(prjBoardStore.tabsStore.activeKey);

            return (
                <div onClick={() => prjBoardStore.showSlideModal(false)}>
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@n17hue`}
                        datas={this.getRightMenu()}
                        onClick={this.onRightMenuClick.bind(this)}
                    >
                    
                        <div style={{ height: "100%", overflow: "hidden" }}>
                            
                            <div style={{ height: "calc(100% - 47px)" }}  >
                                <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@0p8vq5`} height={"100%"} >
                                    {
                                        typeof viewObj === "object" ? viewObj :
                                            <iframe src={viewObj} id="prjtabiframe" name="prjtabiframe" className="flowFrame" frameborder="0" width="100%" height="100%" />
                                    }
                                </WeaNewScroll>
                            </div>
                        </div>
                        <div onClick={(e) => e.stopPropagation()} className={"Prj-WeaSlideModal"}>
                            <WeaSlideModal ecId={`${this && this.props && this.props.ecId || ''}_WeaSlideModal@sfk7wd`} visible={prjBoardStore.SlideModalVisible}
                                top={"0px"}
                                width={"800px"}
                                height={"100%"}
                                direction={'right'}
                                measure={''}
                                style={{ overflowY: "hidden" }}
                                content={(prjBoardStore.SlideModalVisible &&
                                    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@yjr9oz`} spinning={prjTaskCardStore.loading} style={{ height: "100%" }}>
                                        <TaskCard ecId={`${this && this.props && this.props.ecId || ''}_TaskCard@x48lrv`} boardStore={prjBoardStore} taskCardStore={prjTaskCardStore} taskid={prjBoardStore.taskid} isNow={prjBoardStore.isNow} />
                                    </Spin>
                                )}
                                onClose={() => prjBoardStore.showSlideModal(false)}
                                onAnimationEnd={() => { }} />

                        </div>
                    </WeaRightMenu>
                </div>
            )
        } else {
            return (<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@m38vgm`} >
                <div style={{ color: '#000' }}>
                    {getLabel(2012, "对不起，您暂时没有权限！")}
                </div>
            </WeaAlertPage>
            )
        }
    }


    getButtons() {
        const { prjBoardStore } = this.props;
        const { prjCardStore, prjid, tabsStore } = prjBoardStore;
        const { rightMenu, form, selectTabKey, taskInfoStore, addProjectStore, shareTableStore, approveplanstatus } = prjCardStore;
        let btnArr = [];
        let { selectedRowKeys } = shareTableStore;

        const key = tabsStore.activeKey
        const tabDatas1 = toJS(tabsStore.tabDatas);

        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            let disabled = false;
            if (tabDatas1) {
                for (let i = 0; i < tabDatas1.length; i++) {
                    if (tabDatas1[i].shortname == "stageset" && tabDatas1[i].key == key && prjCardStore.stageInfo.selectedRowKeys.length == 0) {//项目卡片
                        if (m.type == "BTN_COPY" || m.type == "BTN_DELETE") {
                            disabled = true;
                        }
                    }
                    if (tabDatas1[i].shortname == "prjshare" && tabDatas1[i].key == key && selectedRowKeys.length == 0) {//项目卡片
                        if (m.type == "BTN_COPY" || m.type == "BTN_DELETE") {
                            disabled = true;
                        }
                    }

                    if (tabDatas1[i].shortname == "tasklist" && tabDatas1[i].key == key && approveplanstatus == true) {//项目卡片
                        if (m.type == "BTN_DOSUBMIT") {
                            disabled = true;
                        }
                    }

                    if(tabDatas1[i].shortname  == "tasklist" && tabDatas1[i].key == key && taskInfoStore.selectedRowKeys.length == 0){
                        if(m.type == "BTN_DELETEBATCH"){
                            disabled = true;
                        }
                    }
                }
            }
            m.isTop == '1' && btnArr.length < 4 && btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@hjyvpj@${m.type}`} type="primary"
                    disabled={disabled}
                    onClick={() => {

                        if (tabDatas1) {
                            for (let i = 0; i < tabDatas1.length; i++) {
                                if (tabDatas1[i].shortname == "prjinfo" && tabDatas1[i].key == key) {//项目卡片
                                    if (m.type == "BTN_EDIT") { //编辑
                                        prjCardStore.getPrjInfo({ viewtype: 'edit', prjid: prjid });
                                    } else if (m.type == "BTN_SAVE") {  //保存
                                        prjCardStore.savePrjInfo({ prjid: prjid }, prjBoardStore);
                                    } else if (m.type == "BTN_DELETE") {  //删除
                                        prjCardStore.delPrjInfo({ method: "del", prjid: prjid, from: "mymanagerproject" }, prjBoardStore);
                                    }
                                } else if (tabDatas1[i].shortname == "tasklist" && tabDatas1[i].key == key) { //任务列表
                                    if (m.type == "BTN_ADDTASK") {  //添加任务
                                        taskInfoStore.handleDialog(true, "add", '', { prjid: prjid, parentid: "" });
                                    } else if (m.type == 'BTN_EDIT') { //编辑
                                        prjCardStore.showEditTaskModal(true);
                                    } else if (m.type == 'BTN_DOSUBMIT') { //提交执行
                                        Modal.confirm({
                                            title: getLabel(15172, "系统提示"),
                                            content: getLabel(83919, "你确定要提交执行吗？"),
                                            onOk() {
                                                prjCardStore.doPlanOpt_task({ method: 'approveplan', prjid: prjid }, prjBoardStore);
                                            },
                                            onCancel() { },
                                        });
                                    } else if (m.type == 'BTN_APPSUBMIT') { //提交执行(审批)
                                        Modal.confirm({
                                            title: getLabel(15172, "系统提示"),
                                            content: getLabel(83921, "你确定要提交审批吗？"),
                                            onOk() {
                                                prjCardStore.doPlanOpt_task({ method: 'submitplan', prjid: prjid }, prjBoardStore);
                                            },
                                            onCancel() { },
                                        });
                                    } else if (m.type == "BTN_DELETEBATCH" ){   //批量删除
                                        taskInfoStore.delTaskBatch("delbatch"); 
                                    }
                                    //  else if (m.type == 'BTN_SENDMESSAGE') { //龙烟二次开发
                                    //     Modal.confirm({
                                    //         title: getLabel(15172, "系统提示"),
                                    //         content:  "你确定要发送通知吗？",
                                    //         onOk() {
                                    //             prjCardStore.sendMsg({prjid: prjid });
                                    //         },
                                    //         onCancel() { },
                                    //     });
                                    // }
                                } else if (tabDatas1[i].shortname == "childprj" && tabDatas1[i].key == key) {//子项目
                                    if (m.type == "BTN_NEWPRJSUB") { //新建子项目
                                        addProjectStore.handleShareDialog(true, { viewtype: 'add', prjid: prjid });
                                    }
                                } else if (tabDatas1[i].shortname == "prjshare" && tabDatas1[i].key == key) {//共享设置
                                    if (m.type == "BTN_APPEND") {  //tianjia
                                        prjCardStore.showAddShareModal(true);
                                    } else if (m.type == 'BTN_DELETEBATCH') { //批量删除
                                        // let { selectedRowKeys } = shareTableStore;
                                        prjCardStore.delBatchShare(`${toJS(selectedRowKeys)}`);
                                    }
                                } else if (tabDatas1[i].shortname == "stageset" && tabDatas1[i].key == key) {  //阶段
                                    if (m.type == 'BTN_ADD_SHARE') { //新增
                                        this.refs.StageList.tableEdit.refs.edit.doAdd()
                                    } else if (m.type == "BTN_DELETE") {  //删除
                                        this.refs.StageList.tableEdit.refs.edit.doDelete()
                                    } else if (m.type == "BTN_COPY") {  //复制
                                        this.refs.StageList.tableEdit.refs.edit.doCopy()
                                    } else if (m.type == "BTN_SUBMIT") {  //保存
                                        const checkProps = this.refs.StageList.tableEdit.refs.edit.doRequiredCheck();
                                        prjCardStore.saveStageSet(checkProps, prjid);
                                    }
                                }
                            }
                        }
                    }}>
                    {m.menuName}
                </Button>
            );
        });
        return btnArr;
    }

    getRightMenu = () => {
        const { prjBoardStore } = this.props;
        const { prjCardStore, tabsStore } = prjBoardStore;
        let btnArr = [];
        const { rightMenu,  taskInfoStore,  approveplanstatus } = prjCardStore;

        const key = tabsStore.activeKey
        const tabDatas1 = toJS(tabsStore.tabDatas);

        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            let disabled = false;
            let { selectedRowKeys } = prjCardStore.shareTableStore;
            if (tabDatas1) {
                for (let i = 0; i < tabDatas1.length; i++) {
                    if (tabDatas1[i].shortname == "stageset" && tabDatas1[i].key == key && prjCardStore.stageInfo.selectedRowKeys.length == 0) {//项目卡片
                        if (m.type == "BTN_COPY" || m.type == "BTN_DELETE") {
                            disabled = true;
                        }
                    }
                    if (tabDatas1[i].shortname == "prjshare" && tabDatas1[i].key == key && selectedRowKeys.length == 0) {//项目卡片
                        if (m.type == "BTN_COPY" || m.type == "BTN_DELETE") {
                            disabled = true;
                        }
                    }

                    if (tabDatas1[i].shortname == "tasklist" && tabDatas1[i].key == key && approveplanstatus == true) {//项目卡片
                        if (m.type == "BTN_DOSUBMIT") {
                            disabled = true;
                        }
                    }

                    if(tabDatas1[i].shortname  == "tasklist" && tabDatas1[i].key == key && taskInfoStore.selectedRowKeys.length == 0){
                        if(m.type == "BTN_DELETEBATCH"){
                            disabled = true;
                        }
                    }
                }
            }
            btnArr.push({
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
                disabled: disabled
            })
        });
        return btnArr
    }

    onRightMenuClick(keys) {
        let that = this;
        const { prjBoardStore } = this.props;
        const { prjCardStore, prjid, tabsStore } = prjBoardStore;
        const { rightMenu, taskInfoStore, addProjectStore, shareTableStore, relateListStore } = prjCardStore;
        const selectTabKey = prjBoardStore.tabsStore.activeKey;

        const key = tabsStore.activeKey
        const tabDatas1 = toJS(tabsStore.tabDatas);

        rightMenu && rightMenu.length > 0 && rightMenu.map((m, i) => {
            if (Number(keys) == i) {
                if (tabDatas1) {
                    for (let i = 0; i < tabDatas1.length; i++) {
                        if (tabDatas1[i].shortname == "prjinfo" && tabDatas1[i].key == key) {//项目卡片
                            let fn = m.menuFun.indexOf('this') >= 0 ? `${m.menuFun.split('this')[0]})` : m.menuFun;
                            if (fn != "") {
                                fn = fn.substring(0, fn.indexOf('('));
                                if ('onNormal' == fn) {   //正常
                                    prjCardStore.doPlanOpt({ method: 'normal', prjid: prjid }, prjBoardStore);
                                } else if ('onOver' == fn) {   //延期
                                    prjCardStore.doPlanOpt({ method: 'delay', prjid: prjid }, prjBoardStore);
                                } else if ('onFinish' == fn) {   //完成
                                    prjCardStore.doPlanOpt({ method: 'complete', prjid: prjid }, prjBoardStore);
                                } else if ('onFrozen' == fn) {   //冻结
                                    prjCardStore.doPlanOpt({ method: 'freeze', prjid: prjid }, prjBoardStore);
                                } else if ('onToggleStatus' == fn) {
                                    prjCardStore.doPlanOpt({ method: 'togglestatus', prjid: prjid, statusid: m.params }, prjBoardStore);
                                }
                            } else {
                                if (m.type == "BTN_EDIT") { //编辑
                                    prjCardStore.getPrjInfo({ viewtype: 'edit', prjid: prjid });
                                } else if (m.type == "BTN_SAVE") {  //保存
                                    prjCardStore.savePrjInfo({ prjid: prjid }, prjBoardStore);
                                } else if (m.type == "BTN_NEWREQ") {  //新建流程
                                    window.open("/spa/workflow/static/index.html#/main/workflow/add?prjid=" + prjid, "_blank");
                                } else if (m.type == "BTN_NEWDOC") {  //新建文档
                                    window.open("/spa/document/static/index.html#/main/document/add?moudleFrom=prj&prjid=" + prjid, "_blank");
                                } else if (m.type == "BTN_NEWRQ") {  //新建日程
                                    this.setState({ workPlanVisible: true })
                                } else if (m.type == "BTN_NEWMT") {  //新建会议
                                    window.open("/spa/meeting/static/index.html#/main/meeting/dialogsingle?type=create&projectid=" + prjid, "_blank");
                                } else if (m.type == "BTN_SAVETEMPLET") {  //另存为模板
                                    prjCardStore.setVisiable(true);
                                } else if (m.type == "BTN_BACK") { //返回
                                    prjCardStore.getPrjInfo({ viewtype: 'view', prjid: prjid });
                                } else if (m.type == "BTN_LOG") {
                                    this.refs.projectLogDialog.setVisible(true);
                                    prjCardStore.getPrjLogList({ prjid: prjid });
                                } else if (m.type == "BTN_DELETE") {
                                    prjCardStore.delPrjInfo({ method: "del", prjid: prjid, from: "mymanagerproject" }, prjBoardStore);
                                }

                            }
                        } else if (tabDatas1[i].shortname == "tasklist" && tabDatas1[i].key == key) {//任务列表
                            if (m.type == "BTN_ADDTASK") {  //添加任务
                                taskInfoStore.handleDialog(true, "add", '', { prjid: prjid, parentid: "" });
                            } else if (m.type == 'BTN_EDIT') { //编辑
                                prjCardStore.showEditTaskModal(true);
                            } else if (m.type == 'BTN_DOSUBMIT') { //提交执行
                                Modal.confirm({
                                    title: getLabel(15172, "系统提示"),
                                    content: getLabel(83919, "你确定要提交执行吗？"),
                                    onOk() {
                                        prjCardStore.doPlanOpt_task({ method: 'approveplan', prjid: prjid }, prjBoardStore);
                                    },
                                    onCancel() { },
                                });
                            } else if (m.type == 'BTN_APPSUBMIT') { //提交执行(审批)
                                Modal.confirm({
                                    title: getLabel(15172, "系统提示"),
                                    content: getLabel(83921, "你确定要提交审批吗？"),
                                    onOk() {
                                        prjCardStore.doPlanOpt_task({ method: 'submitplan', prjid: prjid }, prjBoardStore);
                                    },
                                    onCancel() { },
                                });
                            } else if (m.type == 'BTN_TASKEXPORT') { //导出任务
                                prjCardStore.doTaskExcelExp({ prjid: prjid });
                            } else if (m.type == 'BTN_SAVEPLAN') { //存为计划版本
                                prjCardStore.saveasplan({ method: 'saveasplan', prjid: prjid });
                            } else if (m.type == 'BTN_TASKHISTORY') { //历史版本
                                relateListStore.handleRelateDialog(true, "history", prjid)
                            } else if (m.type == 'BTN_NOTEMEMBER') { //通知成员
                                relateListStore.handleRelateDialog(true, "notice", prjid)
                            } else if (m.type == 'BTN_ONREQUEST') { //相关流程
                                relateListStore.handleRelateDialog(true, "request", prjid)
                            } else if (m.type == 'BTN_ONDOC') { //相关文档
                                relateListStore.handleRelateDialog(true, "doc", prjid)
                            } else if (m.type == 'BTN_ONCOWORK') { //相关协作
                                relateListStore.handleRelateDialog(true, "cowork", prjid)
                            } else if (m.type == 'BTN_LOG') {  //日志
                                relateListStore.handleRelateDialog(true, "log", prjid)
                            } else if (m.type == "BTN_DELETEBATCH" ){   //批量删除
                                taskInfoStore.delTaskBatch("delbatch"); 
                            }
                            // else if (m.type == 'BTN_SENDMESSAGE') { //龙烟二次开发
                            //     Modal.confirm({
                            //         title: getLabel(15172, "系统提示"),
                            //         content:  "你确定要发送通知吗？",
                            //         onOk() {
                            //             prjCardStore.sendMsg({prjid: prjid });
                            //         },
                            //         onCancel() { },
                            //     });
                            // }
                        } else if (tabDatas1[i].shortname == "childprj" && tabDatas1[i].key == key) {//子项目
                            if (m.type == "BTN_NEWPRJSUB") { //新建子项目
                                addProjectStore.handleShareDialog(true, { viewtype: 'add', prjid: prjid });
                            }
                        } else if (tabDatas1[i].shortname == "prjshare" && tabDatas1[i].key == key) {//共享设置
                            if (m.type == "BTN_APPEND") {  //tianjia
                                prjCardStore.showAddShareModal(true);
                            } else if (m.type == 'BTN_DELETEBATCH') { //批量删除
                                let { selectedRowKeys } = shareTableStore;
                                prjCardStore.delBatchShare(`${toJS(selectedRowKeys)}`);
                            }
                        } else if (tabDatas1[i].shortname == "stageset" && tabDatas1[i].key == key) {  //阶段
                            if (m.type == 'BTN_ADD_SHARE') { //新增
                                this.refs.StageList.tableEdit.refs.edit.doAdd()
                            } else if (m.type == "BTN_DELETE") {  //删除
                                this.refs.StageList.tableEdit.refs.edit.doDelete()
                            } else if (m.type == "BTN_COPY") {  //复制
                                this.refs.StageList.tableEdit.refs.edit.doCopy()
                            } else if (m.type == "BTN_SUBMIT") {  //保存
                                const checkProps = this.refs.StageList.tableEdit.refs.edit.doRequiredCheck();
                                prjCardStore.saveStageSet(checkProps, prjid);
                            }
                        }
                        if (m.type == "BTN_COLUMN") {  //显示定制
                            prjCardStore.onShowColumn();
                        }
                    }
                }
            }
        });
    }

    
    setWorkPlanVisible = (bool) => {
        this.setState({ workPlanVisible: bool })
    }
}

export default TaskBoard;